/*A Simple Calculator.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char ExpressionEnter[60];

int Wrong(void);
int Dispatchpression(void);							//将字符串数据填充到链表 
void Multiplication(void);							//乘除法运算 
void AdditionOperation(void);						//加减法运算 

typedef struct expression
{
	double Number;
	char Mark;
	struct expression *Last;
	struct expression *Next;
}Expression;					//表达式结构体定义 

Expression *Head = NULL;		//链表头指针 

int main(void)
{
	char continuevar;
	system("color 0f");
	printf("**********************四则运算器**********************\n");
	printf("表达式规范:比如'3+2-5*0=[Enter]'\n\n");
	while(1)
	{
		ex: 
		scanf("%s",ExpressionEnter);
		if(Wrong())
		{ 
			DispatchExpression();
			Multiplication();
			AdditionOperation();
			printf("%.2lf\n\n",Head->Number);
			printf("再次计算?(N键退出)\n");
			scanf("%c",&continuevar);
			if(continuevar == 'N' || continuevar == 'n')
			{
				break;
			}
		}
		else
		{
			printf("表达式非法请重新输入:\n");
			ExpressionEnter[0] = '\0';
			goto ex;
		}
	}
}

void Multiplication(void)
{
	struct expression *pTmp = Head;
	struct expression *Next;
	double Tmp;
	while(pTmp)
	{
		if(pTmp->Mark == '*')
		{
			Tmp = pTmp->Last->Number * pTmp->Next->Number;
			((pTmp->Last)->Number) = Tmp;
			pTmp = pTmp->Last;
			Next = pTmp->Next;
			pTmp->Next = ((pTmp->Next)->Next)->Next;
			free(Next); 
			pTmp->Next->Last = pTmp;
		}
		else if(pTmp->Mark == '/')
		{
			(pTmp->Last)->Number = (pTmp->Last)->Number / (pTmp->Next)->Number;
			pTmp = pTmp->Last;
			Next = pTmp->Next;
			pTmp->Next = ((pTmp->Next)->Next)->Next;
			free(Next);
			pTmp->Next->Last = pTmp;
		}
		pTmp = pTmp->Next;
	}
	return;
}

void AdditionOperation(void)
{
	struct expression *pTmp = Head;
	struct expression *Next;
	double Tmp;
	while(pTmp)
	{
		if(pTmp->Mark == '+')
		{
			Tmp = pTmp->Last->Number + pTmp->Next->Number;
			((pTmp->Last)->Number) = Tmp;
			pTmp = pTmp->Last;
			Next = pTmp->Next;
			pTmp->Next = ((pTmp->Next)->Next)->Next;
			free(Next); 
			pTmp->Next->Last = pTmp;
		}
		else if(pTmp->Mark == '-')
		{
			(pTmp->Last)->Number = (pTmp->Last)->Number - (pTmp->Next)->Number;
			pTmp = pTmp->Last;
			Next = pTmp->Next;
			pTmp->Next = ((pTmp->Next)->Next)->Next;
			free(Next);
			pTmp->Next->Last = pTmp;
		}
		pTmp = pTmp->Next;
	}
	return;
}

int DispatchExpression(void)
{
	char Tmp[60];
	int ImmortalA,ImmortalT;
	Expression* pTmp;
	for(ImmortalA = ImmortalT = 0;ExpressionEnter[ImmortalA] != '\0';ImmortalA++,ImmortalT++)
	{
		if(ExpressionEnter[ImmortalA] == '+'||ExpressionEnter[ImmortalA] == '='||ExpressionEnter[ImmortalA] == '-'||ExpressionEnter[ImmortalA] == '*'||ExpressionEnter[ImmortalA] == '/')
		{
			Tmp[ImmortalT] = '\0';
			if(Head == NULL)
			{
				Head = (Expression*)malloc(sizeof(Expression));
				Head->Last = Head;
				Head->Next = NULL;
				Head->Number = atof(Tmp);
				Head->Mark = 0;
				pTmp = Head;
			}
			else
			{
				pTmp->Next = (Expression*)malloc(sizeof(Expression));
				(pTmp->Next)->Last = pTmp;
				(pTmp->Next)->Next = NULL;
				pTmp->Next->Number = atof(Tmp);
				pTmp->Next->Mark = 0;
				pTmp = pTmp->Next;
			}
			pTmp->Next = (Expression*)malloc(sizeof(Expression));
			(pTmp->Next)->Last = pTmp;
			(pTmp->Next)->Next = NULL;
			pTmp->Next->Mark =  ExpressionEnter[ImmortalA];
			pTmp = pTmp->Next;
			ImmortalT = -1;
		}
		Tmp[ImmortalT] = ExpressionEnter[ImmortalA];
	}
	return 0;
}

int Wrong(void)
{
	int index;
	int count = 0;
	for(index = 0;ExpressionEnter[index] != '\0';index++)
	{
		if(ExpressionEnter[index] == '=')
		{
			count++;
		}
	}
	if(count == 0)
	{
		return 0;
	}
	return 1;
}
